﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using RepertorySystem.Models;
using System.Data.Entity.Validation;
using System.Diagnostics;

namespace RepertorySystem.Controllers {
    [Authorize]
    public class InputStoragesController : Controller {
        private InputStorageDbContext db = new InputStorageDbContext();

        // GET: InputStorages
        public ActionResult Index() {
            int arryLength = db.InputStorages.Count();
            int totalPages;
            if (arryLength % 5 != 0) {
                totalPages = (arryLength / 5) + 1;
            } else {
                totalPages = (arryLength / 5);
            }

            List<InputStorage> content = db.InputStorages.OrderByDescending(u => u.Time).Skip(0).Take(5).ToList();
            ViewBag.content = content;
            ViewBag.totalPages = totalPages;
            ViewBag.currentPage = 1;

            return View();
        }


        public ActionResult GetNext(int currentPage) {
            int arryLength = db.InputStorages.Count();
            int totalPages;
            if (arryLength % 5 != 0) {
                totalPages = (arryLength / 5) + 1;
            } else {
                totalPages = (arryLength / 5);
            }
            

            List<InputStorage> content = db.InputStorages.OrderByDescending(u => u.Time).Skip((currentPage - 1) * 5).Take(5).ToList();
            ViewBag.content = content;
            ViewBag.currentPage = currentPage;
            ViewBag.totalPages = totalPages;
            return View("Index");

        }

        public ActionResult FindOne(String book_Name) {
            //查询数据
            this.TempData["book_Name"] = book_Name;
            //跳转
            return RedirectToAction("FindOneInputStorage", "BookModels", "");

        }

        public ActionResult ReturnOne() {
            BookModels book = new BookModels();
            book = this.TempData["resultBook"] as BookModels;
            int id = book.ID;

            List<InputStorage> inputStorage = db.InputStorages.Where(a => a.Book_Id == book.ID).ToList();



            ViewBag.content = inputStorage;
            ViewBag.currentPage = 1;
            ViewBag.totalPages = 1;


            return View("Index");
        }

        public ActionResult ReturnNoFind() {
            ViewBag.content = null;
            ViewBag.currentPage = 0;
            ViewBag.totalPages = 0;
            return View("Index");
        }

        // GET: InputStorages/Details/5
        public ActionResult Details(int? id) {
            if (id == null) {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            InputStorage inputStorage = db.InputStorages.Find(id);
            if (inputStorage == null) {
                return HttpNotFound();
            }
            return View(inputStorage);
        }

        // GET: InputStorages/Create
        public ActionResult Create() {
            if (this.TempData != null) {
                AccountList accountLists = this.TempData["accountList"] as AccountList;
                //添加新入库单
                InputStorage inputStorage = new InputStorage();
                inputStorage.Time = accountLists.Time;
                inputStorage.Book_Id = accountLists.Book_Id;
                inputStorage.Number = accountLists.Book_Number;
                inputStorage.Price = accountLists.Price;
                //保存数据
              
                db.InputStorages.Add(inputStorage);
                db.SaveChanges();
             

                //将入库信息传递到库存控制器
                StorageList storageList = new StorageList();
                storageList.Book_Id = accountLists.Book_Id;
                
                //如果为出库
                if(accountLists.Category == 0) {
                    storageList.Book_Number = - accountLists.Book_Number;
                } else {
                    storageList.Book_Number = accountLists.Book_Number;
                }
                

                this.TempData["storageList"] = storageList;
                return RedirectToAction("Create", "StorageLists", "");
            }
            return View();
        }

        // POST: InputStorages/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,Supplier,Book_Id,Number,Time,Price")] InputStorage inputStorage) {
            if (ModelState.IsValid) {
                db.InputStorages.Add(inputStorage);
                db.SaveChanges();
            }

            return View(inputStorage);
        }

        // GET: InputStorages/Edit/5
        public ActionResult Edit(int? id) {
            if (id == null) {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            InputStorage inputStorage = db.InputStorages.Find(id);
            if (inputStorage == null) {
                return HttpNotFound();
            }
            return View(inputStorage);
        }

        // POST: InputStorages/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,Supplier,Book_Id,Number,Time,Price")] InputStorage inputStorage) {
            if (ModelState.IsValid) {
                db.Entry(inputStorage).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(inputStorage);
        }

        // GET: InputStorages/Delete/5
        public ActionResult Delete(int? id) {
            if (id == null) {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            InputStorage inputStorage = db.InputStorages.Find(id);
            if (inputStorage == null) {
                return HttpNotFound();
            }
            return View(inputStorage);
        }

        // POST: InputStorages/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id) {
            InputStorage inputStorage = db.InputStorages.Find(id);
            db.InputStorages.Remove(inputStorage);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing) {
            if (disposing) {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        //private List<SelectListItem> GetBookList() {
        //    var Books = db.InputStorages.OrderBy(m => m.Major).Select(m => m.Major).Distinct();

        //    var items = new List<SelectListItem>();
        //    foreach (string major in majors) {
        //        items.Add(new SelectListItem {
        //            Text = major,
        //            Value = major
        //        });
        //    }
        //    return items;
        //}
    }
}
